package controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import dao.LogDao;
import dao.UsersDao;
import util.MybatisSessionFactory;

@Controller
public class UsersController {
	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		LogDao daol = Session.getMapper(LogDao.class);
		Map<String, Object> result = dao.query(account);

		if ("1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		dao.add(account, password, "2078-09-09");
		Map<String, Object> map = dao.lastid();
		daol.adduser_log(map.get("id").toString(), "0", "注册");
		Session.commit();
		return "OK";
	}

	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response// 从后台到前台用response 从前台到后台用request
	) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		LogDao dao2 = Session.getMapper(LogDao.class);
		Map<String, Object> result = dao.queryone(account, password);
		if ("0".equals(result.get("counter").toString())) {
			return "ERR";
		}
		// result是存于浏览器cookie的名字
		response.addCookie(new Cookie("result", result.get("id").toString()));
		dao2.adduser_log(result.get("id").toString(), "0", "登录");
		Session.commit();
		return "OK";
	}

	@RequestMapping(value = "/query_nickname.action", method = RequestMethod.GET)
	@ResponseBody
	// 把数据直接返回到浏览器上
	public Map<String, Object> querynick(
			@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response

	) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		Map<String, Object> result = dao.query_nickname(userId);
		return result;
	}

	@RequestMapping(value = "/query_income.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> query_income(
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map<String, Object>> result = dao.query_income(userId);
		return result;
	}

	@RequestMapping(value = "/query_pay.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> query_pay(
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map<String, Object>> result = dao.query_pay(userId);
		return result;
	}

	@RequestMapping(value = "/query_week.action", method = RequestMethod.GET)
	@ResponseBody
	// 把数据直接返回到浏览器上
	public Map<String, Object> query_week(
			@RequestParam(value = "id", defaultValue = "") String id) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);

		Map<String, Object> map = dao.query_week("1001");
		return map;

	}

	@RequestMapping(value = "querysr.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> querysr(
			@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		// System.out.println(userId);
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UsersDao d = session.getMapper(UsersDao.class);
		List<Map> map = d.querysr(userId);
		return map;

	}

	@RequestMapping(value = "queryzc.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> queryzc(
			@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UsersDao d = session.getMapper(UsersDao.class);
		List<Map> map = d.queryzc(userId);
		return map;

	}

	@RequestMapping(value = "newaddout.action", method = RequestMethod.POST)
	@ResponseBody
	public String newaddout(
			@CookieValue(value = "result", defaultValue = "") String userId,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		LogDao dao2 = Session.getMapper(LogDao.class);
		Map<String, Object> lastid = dao.lastid();
		dao.newaddout(userId, date_time, place, money_out, kind, remark);
		dao2.adduser_log(userId, lastid.get("id").toString(), "支出");
		Session.commit();
		return "OK";

	}

	@RequestMapping(value = "newaddin.action", method = RequestMethod.POST)
	@ResponseBody
	public String newaddin(
			@CookieValue(value = "result", defaultValue = "") String userId,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		LogDao dao2 = Session.getMapper(LogDao.class);
		Map<String, Object> lastid = dao.lastid();

		dao.newaddin(userId, date_time, place, money_in, kind, remark);
		dao2.adduser_log(userId, lastid.get("id").toString(), "收入");

		Session.commit();
		return "OK";

	}

	@RequestMapping(value = "/query_monthin.action", method = RequestMethod.GET)
	@ResponseBody
	// 把数据直接返回到浏览器上
	public Map<String, Object> query_monthin(
			@CookieValue(value = "result", defaultValue = "") String userId,
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);

		Map<String, Object> map = dao.query_monthin(userId);
		return map;

	}

	@RequestMapping(value = "/query_monthout.action", method = RequestMethod.GET)
	@ResponseBody
	// 把数据直接返回到浏览器上
	public Map<String, Object> query_monthout(
			@CookieValue(value = "result", defaultValue = "") String userId,
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);

		Map<String, Object> map = dao.query_monthout(userId);
		return map;

	}

	@RequestMapping(value = "query_avgin.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> query_avgin(
			@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		// System.out.println(userId);
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UsersDao d = session.getMapper(UsersDao.class);
		List<Map<String, Object>> map = d.query_avgin(userId);
		return map;

	}

	@RequestMapping(value = "query_avgout.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> query_avgout(
			@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		// System.out.println(userId);
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UsersDao d = session.getMapper(UsersDao.class);
		List<Map<String, Object>> map = d.query_avgout(userId);
		return map;

	}

	@RequestMapping(value = "queryall.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Map> queryall(
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> map = dao.queryall(userId, date_time, place, money_in,
				money_out, kind, remark);
		return map;

	}

	@RequestMapping(value = "logout.action", method = RequestMethod.POST)
	@ResponseBody
	public String logout(
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		LogDao log = session.getMapper(LogDao.class);
		log.adduser_log(userId, "0", "退出用户");
		response.addCookie(new Cookie("result", null));
		session.commit();
		return "OK";

	}

	@RequestMapping(value = "querydes.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> querydes(

	@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> map = dao.querydes(id, userId);
		Session.commit();
		return map;
	}

	@RequestMapping(value = "querydel.action", method = RequestMethod.POST)
	@ResponseBody
	public String querydel(

	@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		dao.querydel(id);
		LogDao log = Session.getMapper(LogDao.class);
		log.adduser_log(userId, id, "删除");
		Session.commit();
		return "OK";
	}
}
